﻿using System;
using System.Text;

namespace DBMock.Builders
{
    public class StrBuilder : Builder
    {
        private int min; //最小长度
        private int max; //最大长度
        private char[] chars; //字符表

        /// <summary>
        /// 初始化StrBuilder
        /// </summary>
        /// <param name="seed">随机数种子</param>
        /// <param name="minLength">最小长度</param>
        /// <param name="maxLength">最大长度</param>
        /// <param name="chars">字符表</param>
        public StrBuilder(int seed, int minLength, int maxLength, char[] chars)
            : base(seed)
        {
            if (minLength > maxLength)
            { throw new Exception("Minimum is larger than maximum."); }

            min = minLength;
            max = maxLength;
            this.chars = chars;
        }

        public override object Build()
        {
            if (chars == null || chars.Length == 0)
                return string.Empty;

            int charCount = chars.Length;

            int strLength = random.Next(min, max);
            if (strLength == 0) return string.Empty;

            var sb = new StringBuilder();

            for (int i = 0; i < strLength; i++)
            { sb.Append(chars[random.Next(charCount)]); }

            return sb.ToString();
        }
    }
}
